package com.zach.common.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.zach.common.entity.UserFunctionTree;
import com.zach.common.service.UserFunctionTreeService;
import com.zach.util.StrUtil;
@Component
@Scope("request")
public class UserFunctionTreeAction extends BaseWildAction implements ServletResponseAware {
	private static final long serialVersionUID = 3821868473040143810L;
	private UserFunctionTreeService treeService;
	private static Log log = LogFactory.getLog(UserFunctionTreeAction.class.getName());
	private static boolean logDebugFlag = log.isDebugEnabled();
	private HttpServletResponse response;
	
	private Map<String, Object> map;
	
	/**tree node id */
	private String id;
	/** when add node this the parent node id;*/
	private String parentId;
	/**when add brother's node, this indicate the new node's position*/
	private String position;
	/**when add brother's node, this indicate relative node id*/
	private String relativeNode;
	
	
	public String getFunRootJson() throws Exception {
		UserFunctionTree root = treeService.getNode("root");
		treeService.getTreeListAllByParent(root);
		String json = treeService.generateEasyUIJsonTree(root);
		if(logDebugFlag){
			log.debug("\n"+json);
		}
		response.getWriter().print(json);
		return NONE;
	}
	public String getFunNode() throws Exception {
		if(StrUtil.isEmpty(getId())){
			log.error("节点id不能为空。[id]");
			addActionError("节点id不能为空。[id]");
			return ERROR;
		}
		UserFunctionTree tree = treeService.getNode(getId());
		request.setAttribute("tree", tree);
		return SUCCESS;
	}
	
	public String updateFunNodeJson() throws Exception {
		UserFunctionTree tree = new UserFunctionTree();
		fillObjByRequestParams(tree);
		log.debug("upddating tree is :\n\t"+JSONObject.fromObject(tree).toString());
		map = new HashMap<String, Object>();
		try{
			treeService.updateNode(tree);
		}catch (Exception e) {
			log.error("",e);
			map.put("code", JSON_ERROR);
			map.put("msg", e.getMessage());
			response.getWriter().print(JSONObject.fromObject(map).toString());
			return NONE;
		}
		map.put("code", JSON_OK);
		map.put("msg", "更新功能树节点成功");
		map.put("datas", tree);
		response.getWriter().print(JSONObject.fromObject(map).toString());
		return NONE;
	}
	public String addFunNode() throws Exception {
		UserFunctionTree tree = new UserFunctionTree();
		if(!StrUtil.isEmpty(parentId)){
			tree.setId(parentId);
			tree.setParentId(parentId);
			if(logDebugFlag){
				log.debug("新添加功能树 parentId:"+parentId);
			}
		}
		if(!StrUtil.isEmpty(relativeNode)){
			UserFunctionTree brotherNode = treeService.getNode(relativeNode);
			if(brotherNode.getParentId().equals("root")){
				tree.setId("");
			}else{
				tree.setId(brotherNode.getParentId());
			}
			tree.setParentId(brotherNode.getParentId());
			tree.setViewIndex(brotherNode.getViewIndex()+1);
			if(logDebugFlag){
				log.debug("新添加功能树 兄弟节点:"+relativeNode);
			}
		}
		request.setAttribute("tree", tree);
		
		return SUCCESS;
	}
	
	public String saveFunNodeJson() throws Exception {
		UserFunctionTree tree = new UserFunctionTree();
		fillObjByRequestParams(tree);
		log.debug("upddating tree is :\n\t"+JSONObject.fromObject(tree).toString());
		map = new HashMap<String, Object>();
		try{
			treeService.addNode(tree);
			map.put("code", JSON_OK);
			map.put("msg", "新节点添加成功！");
			map.put("datas", tree);
		}catch (Exception e) {
			log.error("",e);
			map.put("code", JSON_ERROR);
			map.put("msg", "添加节点信息失败，"+e.getMessage());
		}
		
		response.getWriter().print(JSONObject.fromObject(map).toString());
		return NONE;
	}
	
	public String delFunNodeJson() throws Exception {
		map = new HashMap<String, Object>();
		
		if(!StrUtil.isEmpty(id)){
			try{
				int i = treeService.delNodesCascade(id);
				map.put("code", JSON_OK);
				map.put("msg", "删除节点成功["+i+"]！");
			}catch (Exception e) {
				log.error("",e);
				map.put("code", JSON_ERROR);
				map.put("msg", "删除节点信息失败，"+e.getMessage());
			}
		}else{
			map.put("code", JSON_ERROR);
			map.put("msg", "请传入正确的参数！");
		}
		response.getWriter().print(JSONObject.fromObject(map).toString());
		return NONE;
	}
	@Override
	public String execute() throws Exception {
		return EMPTY;
	}
	public UserFunctionTreeService getTreeService() {
		return treeService;
	}
	@Resource(name="userFunctionTreeService")
	public void setTreeService(UserFunctionTreeService userFunctionTreeService) {
		this.treeService = userFunctionTreeService;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getParentId() {
		return parentId;
	}
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public String getRelativeNode() {
		return relativeNode;
	}
	public void setRelativeNode(String relativeNode) {
		this.relativeNode = relativeNode;
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}
